<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : User.php
 * Created on : 2021-2-27 12:07:59
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\models;

use think\Model;
use haoteam\traits\JwtAuthModelTrait;
use haoteam\traits\ModelTrait;

/**
 * 
 * Class User
 * Tiger <1192851302@qq.com> 2021-2-27 12:07:59
 */
class User extends Model {

    use JwtAuthModelTrait;
    use ModelTrait;

    /**
     * 电话号码
     * @author Tiger <1192851302@qq.com>
     * @param type $value
     * @param type $data
     * @return type
     */
    public function getMobileCoverAttr($value, $data) {
        if (!empty($data['mobile'])) {
            return getPhoneAttr($data['mobile']);
        }
        return '';
    }

    /**
     * 昵称补全
     * @author Tiger <1192851302@qq.com>
     * @param string $value
     * @param array  $data
     * @return string
     */
    public function getNicknameAttr($value, $data) {
        if (!$value) {
            return getPhoneAttr($data['mobile']);
        }
        return $value;
    }

    /**
     * 用户余额
     * @author Tiger <1192851302@qq.com>
     * @param string $value
     * @param array  $data
     * @return string
     */
    public function getMoneyBalanceAttr($value, $data) {
        return bcadd($data['money'], $data['giveMoney'], 2);
    }

    /**
     * 一对一关联用户验证表
     * @author Tiger <1192851302@qq.com>
     * @return \think\model\relation\HasOne
     */
    public function verify() {
        return $this->hasOne(UserVerify::class, 'user_id', 'id')->field(['user_id', 'fullname', 'identity', 'identity_front_image', 'identity_back_image', 'status'])->bind([
                    'fullname' => 'fullname',
                    'identity', 'identity',
                    'identity_front_image' => 'identity_front_image',
                    'identity_back_image' => 'identity_back_image',
                    'user_id' => 'user_id',
                    'verify_status' => 'status'
        ]);
    }

    /**
     * 一对一关联用户组
     * @author Tiger <1192851302@qq.com>
     * @return \think\model\relation\HasOne
     */
    public function group() {
        return $this->hasOne(UserGroup::class, 'id', 'user_group')->bind([
                    'user_group_name' => 'name'
        ]);
    }

    /**
     * 获取数据列表-分页
     * @author Tiger <1192851302@qq.com>
     * @param array $where
     * @return array
     */
    public static function getList($where) {
        $model = self::alias('a')->leftJoin('user_verify v', 'v.user_id= a.id')->with('group');
        if (!empty($where['search'])) {
            $model->where('v.fullname|a.mobile|b.identity', 'LIKE', '%' . $where['search'] . '%');
        }
        unset($where['search']);
        $model->where($where)->field([
            'a.*',
            'v.fullname', 'v.identity', 'v.identity_front_image', 'v.identity_back_image', 'v.status as verify_status'
        ])->order('id desc');
        $models = $model->paginate(input('limit', 10))->each(function ($item) {
            $item->verify_status_name = isset($item->verify_status) && $item->verify_status == 1 ? '已认证' : '未认证';
            $item->mobile_cover = $item->mobile_cover;
            $item->money_balance = $item->money_balance;
        });
        return [
            'code' => 0,
            'items' => $models->items(),
            'total' => $models->total()
        ];
    }

    /**
     * 
     * @param type $nickname
     * @param type $mobile
     * @param type $openid
     * @param type $password
     * @param type $platform
     * @param type $spread_spid
     * @return type
     */
    public static function register($nickname, $mobile, $openid, $password, $platform, $spread_spid = 0, $user_group = 0, $extra = []) {
        $status = 0;
        //检查是否存在
        $user = self::where(compact('openid'))->find();
        if (!empty($user)) {
            $user->mobile = $mobile;
            $user->user_group = $user_group;
            $user->nickname = $nickname;
            $user->save();
            return $user;
        } else {
            $salt = create_salt();
            $password = encrypt_password($password, $salt);
        }
        return self::create(array_merge(compact('nickname', 'mobile', 'openid', 'password', 'salt', 'spread_spid', 'status', 'user_group', 'platform'), $extra));
    }
}
